﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BattleEngineCommon;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using MarvinsArena.Core;

namespace BattleEngine2D
{
	public class Map : BattleEngineCommon.Map
	{
		public Texture2D texture;
        public Vector2 Position;

		public void LoadContent(ContentManager content)
		{
			Random rand = new Random();
			switch (rand.Next(0, 2))
			{
				case 0: this.texture = content.Load<Texture2D>("Maps/map_wall"); break;
				case 1: this.texture = content.Load<Texture2D>("Maps/map_world"); break;
				case 2: this.texture = content.Load<Texture2D>("Maps/map_scifi"); break;
			}
		}


		public void Draw(SpriteBatch spriteBatch, Camera camera)
		{
			int texPartWidth = this.texture.Width / 2;
			int texPartHeight = this.texture.Height / 2;

			for (int x = 0; x < this.HeightmapLength0; x++)
			{
                //Calculate X position
				int posX = (int)(x * MapScale + camera.Position.X);

				for (int y = 0; y < this.HeightmapLength1; y++)
				{
					//Calculate Y position
                    int posY = (int)(y * MapScale + camera.Position.Y);

					// Skip out of screen elements
					if (posX + MapScale > camera.Screen.X || posX < -MapScale ||
						posY + MapScale > camera.Screen.Y || posY < -MapScale)
						continue;

					if (Heightmap[x, y] == 0)
						spriteBatch.Draw(texture, new Rectangle(posX, posY, (int)MapScale, (int)MapScale),
							new Rectangle(0, 0, texPartWidth, texPartHeight), Color.White);
					else
						spriteBatch.Draw(texture, new Rectangle(posX, posY, (int)MapScale, (int)MapScale),
							new Rectangle(0, texPartHeight, texPartWidth, texPartHeight), Color.White);
				}
			}
		}
	}
}
